﻿using System;
using OpenTK.Graphics.OpenGL;
using OpenTK.Input;
using RayDen.Library.Entity.Interface;

namespace RayDen.GLPlayground
{
    public class GLRenderWindow : MainWindow
    {

        protected GLRenderer RenderConfig
        {
            get { return this.config as GLRenderer; }
        }

        public GLRenderWindow(IRenderConfig cfg) : base(cfg) { }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            this.RenderConfig.SetupOpenGL();
        }

        protected override void OnResize(EventArgs e)
        {
            RenderConfig.SetupView();
            base.OnResize(e);
        }

        protected override void OnUpdateFrame(OpenTK.FrameEventArgs e)
        {
            //Matrix4 rotation = Matrix4.CreateRotationY((float)e.Time);
            //Matrix4.Mult(ref rotation, ref RenderConfig.modelviewMatrix, out RenderConfig.modelviewMatrix);
            //GL.UniformMatrix4(RenderConfig.modelviewMatrixLocation, false, ref RenderConfig.modelviewMatrix);

            if (Keyboard[OpenTK.Input.Key.Escape])
                Exit();

            if (Keyboard[Key.Up])
            {
                RenderConfig.StepForward();
                RenderConfig.SetupView();

                
            }
            if (Keyboard[Key.Down])
            {
                RenderConfig.StepBackward();
                RenderConfig.SetupView();

            }
            base.OnUpdateFrame(e);
        }

        

        protected override void OnRenderFrame(OpenTK.FrameEventArgs e)
        {
            GL.Clear(ClearBufferMask.ColorBufferBit);
            this.RenderConfig.RenderFrame();
            this.Title = this.config.GetStats();
            this.SwapBuffers();
        }
    }
}